<?php
namespace frame\libs;

/**
 * 公共返回对象
 */
class JsonResp {

    /**
     * 返回json结果
     * @param msg
     * @param data
     * @return
     */
    public static function json($data=null, $msg="", $code=200, $count=-1, $transfer = false) {
        if ($count == -1) {
            if (is_array($data)) {
                $count = count($data);
            }
        }
        if ($transfer) {
            return json_encode(array('code' => $code, 'msg' => $msg, 'data' => $data, 'count'=>$count));
        }
        return json_encode(array('code' => $code, 'msg' => $msg, 'data' => $data, 'count'=>$count), JSON_UNESCAPED_UNICODE);
    }

    /**
     * 成功返回结果
     * @param msg
     * @param data
     * @return
     */
    public static function success($data=null, $msg="success", $code=200, $count=-1, $transfer = false) {
        return self::json($data, $msg, $code, $count, $transfer);
    }

    /**
     * 失败返回结果
     * @param msg
     * @param data
     * @return
     */
    public static function error($data=null, $msg="error", $code=500, $count=-1, $transfer = false) {
        return self::json($data, $msg, $code, $count, $transfer);
    }

    /**
     * 无权限返回结果
     * @param  [type]  $data     [description]
     * @param  string  $msg      [description]
     * @param  integer $code     [description]
     * @param  integer $count    [description]
     * @param  boolean $transfer [description]
     * @return [type]            [description]
     */
    public static function forbidden($data=null, $msg="forbidden", $code=403, $count=-1, $transfer = false)
    {
        return self::json($data, $msg, $code, $count, $transfer);
    }

    /**
     * 请求要求用户的身份认证返回结果
     * @param [type]  $data     [description]
     * @param string  $msg      [description]
     * @param integer $code     [description]
     * @param integer $count    [description]
     * @param boolean $transfer [description]
     */
    public static function unauthorized($data=null, $msg="unauthorized", $code=401, $count=-1, $transfer = false)
    {
        return self::json($data, $msg, $code, $count, $transfer);
    }

}

